<?php
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$wxControl = $screen->getChildControl('BUIWXControl_1');
$treeTable = new BTreeTable('BWInputValue');
?>
    
<div style="padding-left: 10px;"><!--class=more-->
    <img src="<?php echo Yii::app()->request->baseUrl . '/images/mobile/fire_ball.png'?>" />
</div>
    <style>
        .recommend td {
            border-color: #dedede;border-style: solid;
        }
        .recommend .timeSlot {
            width: 25%;color:rgb(255, 112, 0);font-weight: bold;border: 1px solid #dedede;
        }
        .recommend .court_type {
            line-height: 40px;color:gray;/*padding: 5px 0;*/border-width: 1px 0;cursor: pointer;
        }
        .recommend .leave_count {
            line-height: 40px;color: #2bb2a3;border-width: 1px 0;cursor: pointer;
        }
        .recommend .leave_count font {
            font-family: fantasy;font-size: x-large;
        }
    </style> 
    <?php
    //设置黄金时间段
    $setStartOfTimeSlot = '>=170000';
    $setEndOfTimeSlot = '<=210000';
    $kingCourtListArr = array();

    foreach ($courtTypeList as $courtType => $courtTypeZn) {
        $valueList1 = $treeTable->getValueList('startTime', array(
            'itemData' => array_merge($itemData,array(
            'startOfTimeSlot' => $setStartOfTimeSlot,
            'endOfTimeSlot' => $setEndOfTimeSlot,
            'startDay' => '<=5',
            'courtType' => $courtType
                ))
            ));
        $valueList2 = $treeTable->getValueList('endTime', array(
            'itemData' => array_merge($itemData,array(
            'startOfTimeSlot' => $setStartOfTimeSlot,
            'endOfTimeSlot' => $setEndOfTimeSlot,
            'startDay' => '<=5',
            'courtType' => $courtType
                ))
            ));
        reset($valueList2);
        foreach ($valueList1 as $dateStamp => $count) {
            $week = date('l',$dateStamp);
            $startOfTimeSlot = date('H:i',$dateStamp);
            $endOfTimeSlot = date('H:i',key($valueList2));

            //筛选出周1-周五的数据
            if(in_array($week,array('Monday', 'Tuesday' ,'Wednesday','Thursday','Friday'))) {
                $week = 'weekday';
                $isInValueArr = false;
                foreach ($kingCourtListArr as $key => $valueArr) {
                    if($valueArr['startOfTimeSlot']==$startOfTimeSlot && $valueArr['endOfTimeSlot']==$endOfTimeSlot && $valueArr['dayType']==$week) {
                        $kingCourtListArr[$key]['courtType'][$courtTypeZn] = $valueArr['courtType'][$courtTypeZn] + $count;
                        $isInValueArr = true;
                        break;
                    }
                }
                if(!$isInValueArr) {
                    /*添加一条数据，如：$kingCourtListArr = array (
                                            ‘0’=>array(‘startTime’=>’17:00’,’endTime’=>’18:00’,"dayType"=> "weekday",’courtType’=>array(
                                                    '11人制足球场地' => 0,
                                                    '7人制足球场地' => 0,
                                                    '5人制足球场地' => 0,
                                                    '羽毛球场地' => 0）
                                        )),*/
                    $kingCourtListArr[] = array('startOfTimeSlot'=>$startOfTimeSlot,'endOfTimeSlot'=>$endOfTimeSlot,'dayType'=> 'weekday','courtType'=>array());
                    foreach ($courtTypeList as $key => $value) {
                        $kingCourtListArr[key($kingCourtListArr)]['courtType'][$value] = 0;
                    }
                    $kingCourtListArr[key($kingCourtListArr)]['courtType'][$courtTypeZn] = intval($count);
                }
            }
            next($valueList2);
        }
    
    

        $valueList3 = $treeTable->getValueList('startTime', array('itemData' => array_merge($itemData,array('startDay' => '>=6','courtType' => $courtType))));
        $valueList4 = $treeTable->getValueList('endTime', array('itemData' => array_merge($itemData,array('startDay' => '>=6','courtType' => $courtType))));
        reset($valueList4);
        foreach ($valueList3 as $dateStamp => $count) {
            $week = date('l',$dateStamp);

            if(in_array($week,array('Saturday', 'Sunday'))) {
//                    $week = 'weekend';
                $isInValueArr = false;
                foreach ($kingCourtListArr as $key => $valueArr) {
                    if($valueArr['dayType']==$week) {
                        $kingCourtListArr[$key]['courtType'][$courtTypeZn] = $valueArr['courtType'][$courtTypeZn] + $count;
                        $isInValueArr = true;
                        break;
                    }
                }
                if(!$isInValueArr) {
                    $startOfTimeSlot = strtotime(date('Y-m-d',$dateStamp).' 00:00:00');
                    $endOfTimeSlot = $startOfTimeSlot + 60 * 60 * 24;
                    $kingCourtListArr[] = array('startOfTimeSlot'=>$startOfTimeSlot,'endOfTimeSlot'=>$endOfTimeSlot,'dayType'=> $week,'courtType'=>array());
                    foreach ($courtTypeList as $key => $value) {
                        $kingCourtListArr[key($kingCourtListArr)]['courtType'][$value] = 0;
                    }
                    $kingCourtListArr[key($kingCourtListArr)]['courtType'][$courtTypeZn] = intval($count);
                }
            }
            next($valueList4);
        }
    }
    ?>

    <?php 
    unset($itemData['bookingStatus']);
    unset($itemData['bookingStartTime']);
    unset($itemData['bookingEndTime']);
    ?>
<dl class="booking_list" style="background-color:white;">
    <dd>
        <br/>
        <p style="color:green;font-weight:bold;padding-left: 10px;">工作日</p>
        <br/>
        <?php
        //循环遍历数组
        foreach ($kingCourtListArr as $key => $valueArr) {
            if($valueArr['dayType'] == 'weekday') {
            //通过设置设置startTime和endTime把时间限定在某一天
            $startOfTimeSlot = $valueArr['startOfTimeSlot'];
            $endOfTimeSlot = $valueArr['endOfTimeSlot'];
            $findText = '';
            foreach ($itemData as $key => $value) {
                $firstChar = substr($value, 0, 1);
                if($firstChar == '=' || $firstChar == '>'|| $firstChar == '<') {
                    $findText = ($findText ? $findText." ".$key.$value : $key.$value);
                }else {
                    $findText = ($findText ? $findText." ".$key."=".$value : $key."=".$value);
                }
            }
            $findText = $findText." startOfTimeSlot=". str_replace(':00', '0000', $startOfTimeSlot)." endOfTimeSlot=". str_replace(':00', '0000', $endOfTimeSlot);

            $flagText = '工作日';
            $findText = $findText.' startDay<=5';
                ?>
                    <table class="recommend" style="width:100%;text-align: center">
                        <tbody>
                        <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_choose_time'), array('backPage' => 'booking_find_1','findText' => $findText.' courtType=soccer_court_11','flagText' => $flagText,'nowCity' => $itemData['city']));?>
                        <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                            <td rowspan="4" class="timeSlot"><?php echo $startOfTimeSlot;?><p style="text-align:center;line-height:10px;">-</p><?php echo $endOfTimeSlot;?></td>
                            <?php if($valueArr['courtType']['11人制足球场地'] != 0) {?>
                            <td class="court_type">11人制足球场地</td>
                            <?php // echo $findText.' courtType=soccer_court_11';?>
                            <td class="leave_count">剩余场地：<font><?php echo $valueArr['courtType']['11人制足球场地']?></font></td>
                            <?php }?>
                        </tr>
                        <?php $courtUrl =  $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_7','flagText' => $flagText,'nowCity' => $itemData['city']));?>
                        <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                            <?php if($valueArr['courtType']['7人制足球场地'] != 0) {?>
                            <td class="court_type">7人制足球场地</td>
                            <td class="leave_count">
                                    剩余场地：<font><?php echo $valueArr['courtType']['7人制足球场地']?></font>
                                </td>
                            <?php }?>
                        </tr>
                        <?php $courtUrl = $wxControl->getLinkUrl(array('name' => $flagText,'id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_5','flagText' => $flagText,'nowCity' => $itemData['city']));?>
                        <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                            <?php if($valueArr['courtType']['5人制足球场地'] != 0) {?>
                            <td class="court_type">5人制足球场地</td>
                            <td class="leave_count">
                                剩余场地：<font><?php echo $valueArr['courtType']['5人制足球场地']?></font>
                                </td>
                            <?php }?>
                        </tr>
                        </tbody>
                    </table>
            <?php 
                }
            }
        ?>   
    </dd>
</dl>
      
<dl class="booking_list" style="background-color:white;">
    <dd>
        <br/>
        <p style="color:green;font-weight:bold;padding-left: 10px;">周末</p>
        <br/>
        <?php
        //循环遍历数组
        foreach ($kingCourtListArr as $key => $valueArr) {
            //通过设置设置startTime和endTime把时间限定在某一天
            $findText = '';
            foreach ($itemData as $key => $value) {
                $firstChar = substr($value, 0, 1);
                if($firstChar == '=' || $firstChar == '>'|| $firstChar == '<') {
                    $findText = ($findText ? $findText." ".$key.$value : $key.$value);
                }else {
                    $findText = ($findText ? $findText." ".$key."=".$value : $key."=".$value);
                }
            }

                if($valueArr['dayType'] == 'Saturday' || $valueArr['dayType'] == 'Sunday') {
                    $displayName = $valueArr['dayType']=='Saturday'?'周六':'周日';
                    $findText = $findText.($valueArr['dayType']=='Saturday'?' startDay=6':' startDay=7');
                ?>
                    <table class="recommend" style="width:100%;text-align: center">
                        <tbody>
                        <?php $courtUrl = $wxControl->getLinkUrl(array('name' => '','id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_11','flagText' => $displayName,'nowCity' => $itemData['city']));?>
                        <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                            <td rowspan="4" class="timeSlot"><?php echo $displayName;?></td>
                            <?php if($valueArr['courtType']['11人制足球场地'] != 0) {?>
                            <td class="court_type">11人制足球场地</td>
                            <td class="leave_count">
                                    剩余场地：<font><?php echo $valueArr['courtType']['11人制足球场地']?></font>
                                </td>
                            <?php }?>
                        </tr>
                        <?php $courtUrl = $wxControl->getLinkUrl(array('name' => '','id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_7','flagText' => $displayName,'nowCity' => $itemData['city']));?>
                        <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                            <?php if($valueArr['courtType']['7人制足球场地'] != 0) {?>
                            <td class="court_type">7人制足球场地</td>
                            <td class="leave_count">
                                    剩余场地：<font><?php echo $valueArr['courtType']['7人制足球场地']?></font>
                                </td>
                            <?php }?>
                        </tr>
                        <?php $courtUrl = $wxControl->getLinkUrl(array('name' => '','id' => 'booking_list'), array('listPage' => 'booking_list_1','findText' => $findText.' courtType=soccer_court_5','flagText' => $displayName,'nowCity' => $itemData['city']));?>
                        <tr onclick="javascript:goRecommend(this,'<?php echo $courtUrl;?>')">
                            <?php if($valueArr['courtType']['5人制足球场地'] != 0) {?>
                            <td class="court_type">5人制足球场地</td>
                            <td class="leave_count"><a href="">
                                剩余场地：<font><?php echo $valueArr['courtType']['5人制足球场地']?></font>
                                </a></td>
                            <?php }?>
                        </tr>
     
                        </tbody>
                    </table>
            <?php 
            }                              
        }
        ?>   
    </dd>
</dl>
